<?php

namespace Module\DataAliyunOss\Core;

use Illuminate\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use ModStart\Admin\Config\AdminMenu;
use ModStart\Data\DataStorageType;
use ModStart\Module\ModuleClassLoader;
use Module\DataAliyunOss\Support\DataAliyunOssDataStorage;

class ModuleServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Dispatcher $events)
    {
        AdminMenu::register(function () {
            return [
                [
                    'title' => L('Site Manage'),
                    'icon' => 'cog',
                    'sort' => 400,
                    'children' => [
                        [
                            'title' => '阿里云OSS存储',
                            'url' => '\Module\DataAliyunOss\Admin\Controller\ConfigController@index',
                        ],
                    ]
                ]
            ];
        });

        ModuleClassLoader::addNamespace('OSS', __DIR__ . '/../SDK/oss-sdk-php/src/OSS');
        if (modstart_config('DataAliyunOss_Enable', false)) {
            $this->app['config']->set('DataStorageDriver', 'DataStorage_DataAliyunOss');
            if (class_exists(DataStorageType::class)) {
                DataStorageType::register('DataAliyunOss', '阿里云OSS云存储');
            }
            $this->app->bind('DataStorage_DataAliyunOss', function () {
                $option = [];
                $option['accessKeyId'] = modstart_config()->getWithEnv('DataAliyunOss_AccessKeyId');
                $option['accessKeySecret'] = modstart_config()->getWithEnv('DataAliyunOss_AccessKeySecret');
                $option['endpoint'] = modstart_config()->getWithEnv('DataAliyunOss_Endpoint');
                $option['bucket'] = modstart_config()->getWithEnv('DataAliyunOss_Bucket');
                $storage = new DataAliyunOssDataStorage($option);
                $storage->init();
                return $storage;
            });
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

    }
}
